package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.RequriedPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper mapper;
    @Autowired
    private ApplicationContext ctx;
    @Override
    public Permission get(Long id) {
        return mapper.get(id);
    }

    @Override
    public List<Permission> list() {
        return mapper.selectAll();
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public PageInfo query(QueryObject qo) {
//        Integer rows = Integer.valueOf(mapper.selectForCount(qo));
//        if(rows.intValue() == 0){
//            return new PageResult(Integer.valueOf(qo.getPageSize()));
//        }
//        List<Permission> list = mapper.selectForList(qo);
//        return new PageResult(Integer.valueOf(qo.getCurrentPage()),rows,list,Integer.valueOf(qo.getPageSize()));
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> list = mapper.selectForList(qo);
        return new PageInfo(list);
    }
    //删除和修改的方法暂时先不写
    @Override
    public void saveOrUpdate(Permission permission) {
        if(permission.getId() == null){
            mapper.insert(permission);
        }else{
            mapper.update(permission);
        }
    }
    //load()加载的实现
    @Override
    public void load() {
        //比较
        //得到权限表中的所有的记录
        List<String> permissions = this.mapper.selectAllExpression();
        //通过applicationContext（上下文）来获取用户的权限，并保存到集合中
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //获取到所有的带注解的类
        Collection<Object> values = map.values();
        //遍历集合
        for(Object controller : values){
            //获取到每个遍历出的controller类
            Class<?> clazz = controller.getClass();
            //获取到每个controller类中的所有方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历用于存储方法的数组
            for (Method method : methods){
                //获取到上面标注了我们自定义注解的方法
                RequriedPermission annotation = method.getAnnotation(RequriedPermission.class);
                if(annotation != null){
                    //获取到注解的权限定名
                    String name = annotation.name();
                    //将权限定名中的包名controller全部替换成空串
                    String simpleName = clazz.getSimpleName().replace("Controller", "");
                    //获取该方法中有自定义注解的表达式值
                    String expression = StringUtils.uncapitalize(simpleName)+":"+method.getName();
                    //开始对比，将对比后的信息，
                    if(!permissions.contains(expression)){
                        System.out.println("permissions =="+permissions);
                        System.out.println("expression =="+expression);
                        //保存
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        this.mapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return mapper.selectByEmpId(id);
    }
}
